﻿#include "StdAfx.h"
#include "MioMacro.h"
#include "MioReader.h"

#include "MioPath.h"
#include "MioPlatform.h"


using namespace mio;
using std::vector;
using std::string;
using std::auto_ptr;

#if _IPHONE_

#pragma mark file reader test
TEST(MioFileReaderTest, read)
{
	string file = path::appPath("asset/readerTest.txt");
	EXPECT_TRUE(file != "");

	FileReader reader;
	string result = reader.read(file);
	string expected("this is line1.\nthis is line2.\n");
	EXPECT_EQ(result, expected);
}

TEST(MioFileReaderTest, readline)
{
	string file = path::appPath("asset/readerTest.txt");
	EXPECT_TRUE(file != "");

	FileReader reader;
	vector<string> result = reader.readline(file);

	ASSERT_EQ(2, result.size());
	EXPECT_STREQ("this is line1.", result[0].c_str());
	EXPECT_STREQ("this is line2.", result[1].c_str());
}

TEST(BufferFileReaderTest, readChar)
{
	string file = path::appPath("asset/readerTest.txt");
	BufferFileReader reader(file, 2);	//의도적으로 작은버퍼 생성후 테스트

	const char *expected = "this is line1.\nthis is line2.\n";
	for(int i = 0 ; i < sizeof(expected) / sizeof(char) ; i++)
	{
		unsigned char ch = reader.readChar();
		unsigned char exCh = expected[i];
		EXPECT_EQ(ch, exCh);
	}
}
TEST(BufferFileReaderTest, filesize)
{
	string file = path::appPath("asset/readerTest.txt");
	int expected = 30;

	BufferFileReader reader(file, 2);	//의도적으로 작은버퍼 생성후 테스트
	int actual = reader.filesize();
	EXPECT_EQ(expected, actual);
}
TEST(BufferFileReaderTest, canRead)
{
	string file = path::appPath("asset/readerTest.txt");
	BufferFileReader reader(file, 2);	//의도적으로 작은버퍼 생성후 테스트
	for(int i = 0 ; i < reader.filesize() ; i++)
	{
		bool read = reader.canRead();
		EXPECT_EQ(true, read);
		reader.readChar();
	}
	//읽기 종료
	for(int i = 0 ; i < 3 ; i++)
	{
		bool read = reader.canRead();
		EXPECT_EQ(false, read);
		unsigned char ch = reader.readChar();
		EXPECT_EQ('\0', ch);
	}
}
TEST(BufferFileReaderTest, canRead1)
{
	string file = path::appPath("asset/readerTest.txt");
	BufferFileReader reader(file, 3);	//의도적으로 작은버퍼 생성후 테스트
	for(int i = 0 ; i < reader.filesize() ; i++)
	{
		bool read = reader.canRead();
		EXPECT_EQ(true, read);
		reader.readChar();
	}
	//읽기 종료
	for(int i = 0 ; i < 3 ; i++)
	{
		bool read = reader.canRead();
		EXPECT_EQ(false, read);
		unsigned char ch = reader.readChar();
		EXPECT_EQ('\0', ch);
	}
}
TEST(BufferFileReaderTest, readLine)
{
	string file = path::appPath("asset/readerTest.txt");
	BufferFileReader reader(file, 2);	//의도적으로 작은버퍼 생성후 테스트
	//read line 1
	EXPECT_EQ(true, reader.canRead());
	string line1 = reader.readLine();
	EXPECT_STREQ("this is line1.", line1.c_str());

	//read line2
	EXPECT_EQ(true, reader.canRead());
	string line2 = reader.readLine();
	EXPECT_STREQ("this is line2.", line2.c_str());

	EXPECT_EQ(false, reader.canRead());
}
TEST(BufferFileReaderTest, readWord)
{
	string file = path::appPath("asset/readerTest.txt");
	BufferFileReader reader(file, 2);	//의도적으로 작은버퍼 생성후 테스트
	string word;

	EXPECT_EQ(true, reader.canRead());
	word = reader.readWord();
	EXPECT_STREQ("this", word.c_str());

	EXPECT_EQ(true, reader.canRead());
	word = reader.readWord();
	EXPECT_STREQ("is", word.c_str());

	EXPECT_EQ(true, reader.canRead());
	word = reader.readWord();
	EXPECT_STREQ("line1.", word.c_str());

	EXPECT_EQ(true, reader.canRead());
	word = reader.readWord();
	EXPECT_STREQ("this", word.c_str());

	EXPECT_EQ(true, reader.canRead());
	word = reader.readWord();
	EXPECT_STREQ("is", word.c_str());

	EXPECT_EQ(true, reader.canRead());
	word = reader.readWord();
	EXPECT_STREQ("line2.", word.c_str());

	EXPECT_EQ(false, reader.canRead());
}
#endif

#if _IPHONE_
#pragma mark xml reader test
TEST(MioXmlReaderTest, read1)
{
	string file = path::appPath("asset/books.xml");
	FileReader reader;
	string content = reader.read(file, 4);	//소형 버퍼로 읽기

	XmlReader xmlreader;
	XmlNodePtr root = xmlreader.read(content);
	EXPECT_TRUE(root.get() != NULL);

	//root의 정보 확인
	EXPECT_STREQ("catalog", root->name().c_str());
	EXPECT_EQ(12, root->childSize());

	//자식은 생긴게 똑같은형태라서 1개만 확인함
	XmlNodePtr child = root->child(0);
	EXPECT_STREQ("book", child->name().c_str());
	EXPECT_EQ(6, child->childSize());
	EXPECT_STREQ("bk101", child->getAttribute("id").c_str());

	//자식 1개씩 확인
	XmlNodePtr inner = child->child(0);
	EXPECT_STREQ("author", inner->name().c_str());
	EXPECT_STREQ("Gambardella, Matthew", inner->content().c_str());

	inner = child->child(5);
	EXPECT_STREQ("description", inner->name().c_str());
	EXPECT_STREQ("An in-depth look at creating applications with XML.", inner->content().c_str());
}
TEST(MioXmlReaderTest, read2)
{
	string file = path::appPath("asset/monster.xml");
	mio::FileReader reader;
	string content = reader.read(file);

	mio::XmlReader xmlreader;
	mio::XmlNodePtr root = xmlreader.read(content);

	mio::XmlNodeList monsterNodeList = root->getElementsByName("monster");
	mio::XmlNodePtrIter iter = monsterNodeList.begin();
	for( ; iter != monsterNodeList.end() ; iter++)
	{
		mio::XmlNodePtr nodePtr = *iter;
		mio::XmlNodePtrIter childIter;
		for(childIter = nodePtr->childBegin() ; childIter != nodePtr->childEnd() ; childIter++)
		{
			mio::XmlNodePtr attrNode = *childIter;
			const std::string &key = attrNode->getAttribute("key");
			const std::string &value = attrNode->getAttribute("value");
			EXPECT_TRUE(key.length() > 0);
			EXPECT_TRUE(value.length() > 0);
		}
	}
}
#endif